<template>
    <div class="ct-bodySet">
        <el-container>
            <el-header class="bgclr-dk-sp0 mb20 headerCoster">
                <el-breadcrumb separator-class="el-icon-arrow-right">
                    <el-breadcrumb-item :to="{ path: '/mainPage/welcome' }"
                        >首页</el-breadcrumb-item
                    >
                    <el-breadcrumb-item>扫描安全问题</el-breadcrumb-item>
                    <el-breadcrumb-item
                        ><span class="main-Clr-ft"
                            >信息查询</span
                        ></el-breadcrumb-item
                    >
                </el-breadcrumb>
            </el-header>
            <div class="pl20 pr20">
                <el-card class="mb20 cardWidth">
                    <el-form
                        :inline="true"
                        :model="formData"
                        ref="formData"
                        class="noMarginBottom"
                    >
                        <el-form-item class="mb1" label="年度：">
                            <el-select
                                v-model="formData.year"
                                placeholder="请选择"
                            >
                                <el-option
                                    v-for="item in year"
                                    :label="item.value"
                                    :value="item"
                                    :key="item.code"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item class="mb1" label="月份：">
                            <el-select
                                v-model="formData.month"
                                placeholder="请选择"
                            >
                                <el-option
                                    v-for="item in month"
                                    :label="item.value"
                                    :value="item"
                                    :key="item.code"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item class="mb1" label="是否解决：">
                            <el-select
                                v-model="formData.isFlag"
                                placeholder="请选择"
                            >
                                <el-option
                                    v-for="item in isFlag"
                                    :label="item.value"
                                    :value="item"
                                    :key="item.code"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-button type="primary" @click="queryList()"
                            >查询</el-button
                        >
                    </el-form>
                </el-card>
                <el-card class="mb20 cardWidth">
                    <template>
                        <template>
                             <!--搜索 -->                            
                            <el-popover
                                placement="bottom"
                                trigger="click"
                            >
                                <el-input v-model="search"  placeholder="输入关键字搜索"/>
                                <el-button
                                        class="el-icon-search"
                                        type="text"
                                        size="medium"
                                        slot="reference"
                                >
                                    <span>搜索</span>
                                </el-button>
                            </el-popover>
                             <!--字段管理 -->
                            <el-popover
                                placement="bottom"
                                title="字段管理"
                                trigger="click"
                            >
                                <el-checkbox-group
                                    v-model="checkList"
                                    @change="columnFilter"
                                >
                                    <el-checkbox
                                        v-for="(item, index) in tableList"
                                        :label="item.label"
                                        >{{ item.value }}</el-checkbox
                                    >
                                </el-checkbox-group>
                                <el-button
                                    style="padding-left:10px" 
                                    class="el-icon-tickets"
                                    type="text"
                                    size="medium"
                                    slot="reference"
                                >
                                    <span>字段管理</span>
                                </el-button>
                            </el-popover>
                             <!--导出 -->
                            <span>
                                <img style="padding-top:9px;padding-left:10px" src="static/icon/导出.png"/>
                                <el-button 
                                    type="text"  
                                    size="medium" 
                                    slot="reference"
                                    @click="htmlToExcel('#printTest', '表格')">
                                    导出Excel
                                </el-button>
                            </span>
                        </template>              
                        <el-table
                            ref="tableCot"
                            border
                            stripe
                            :header-cell-style="getRowClass"
                            :data="securityList.filter(data => !search || data.zj.toLowerCase().includes(search.toLowerCase()))"
                            style="width: 100%"
                            :height="height"
                            :key="key"
                            id="printTest"
                        >
                            <el-table-column
                                prop="aqsjlx"
                                label="安全事件类型"
                                width="105"
                                v-if="columnShow.aqsjlx"
                            >
                            </el-table-column>
                            <el-table-column
                                prop="sjms"
                                label="事件描述"
                                width="110"
                                v-if="columnShow.sjms"
                            >
                            </el-table-column>
                            <el-table-column
                                prop="zj"
                                label="组件"
                                v-if="columnShow.zj"
                            >
                            </el-table-column>
                            <el-table-column
                                width="120"
                                prop="fssj_year"
                                label="发生时间（年）"
                                v-if="columnShow.fssj_year"
                            >
                            </el-table-column>
                            <el-table-column
                               width="120"
                                prop="fssj_month"
                                label="发生时间（月）"
                                v-if="columnShow.fssj_month"
                            >
                            </el-table-column>
                            <el-table-column
                               width="100"
                                prop="fscs"
                                label="发生次数"
                                v-if="columnShow.fscs"
                            >
                            </el-table-column>
                            <el-table-column
                               width="150"
                                prop="cxzsc"
                                label="持续总时长（分钟）"
                                v-if="columnShow.cxzsc"
                            >
                            </el-table-column>
                            <el-table-column
                             width="150"
                                prop="pjcxsc"
                                label="平均持续时长(分钟)"
                                v-if="columnShow.pjcxsc"
                            >
                            </el-table-column>
                            <el-table-column
                                prop="dqzt"
                                label="当前状态"
                                width="80"
                                v-if="columnShow.dqzt"
                            >
                            </el-table-column>
                            <el-table-column
                                prop="blxm"
                                label="暴露项目"
                                v-if="columnShow.blxm"
                            >
                            </el-table-column>
                            <el-table-column
                              width="130"
                                prop="zyclfs"
                                label="主要处理方式"
                                v-if="columnShow.zyclfs"
                            >
                            </el-table-column>
                            <el-table-column
                                fixed="right"
                                prop="sfjj"
                                label="是否解决"
                                width="80"
                                v-if="columnShow.sfjj"
                            >
                            </el-table-column>
                             <!--自定义无数据显示 -->
                            <div slot="empty">
                                <p>
                                    <img src="static/icon/无搜索结果.png"/>
                                </p>
                                <p style="padding-top:40px"><span>暂无数据</span></p>
                            </div>
                        </el-table>
                    </template>
                    <el-col :span="24">
                        <div><br /></div>
                    </el-col>
                </el-card>
            </div>
            <div class="bottomLayoutBus"></div>
        </el-container>
    </div>
</template>

<script>
import urlConfig from "../../../config/url.json";
import Vue from "vue";
import { Message } from "element-ui";
import FileSaver from "file-saver";
import XLSX from "xlsx";
Vue.component(Message.name, Message);
let querySecurityQuestionUrl = urlConfig.querySecurityQuestion;
export default {
    name: "",
    data() {
        return {
            formData: {
                year: "",
                month: "",
                isFlag: "",
            },
            year: [
                {
                    code: "1",
                    value: "2022",
                },
                {
                    code: "2",
                    value: "2021",
                },
                {
                    code: "3",
                    value: "2020",
                },
            ],
            month: [
                {
                    code: "1",
                    value: "1月份",
                },
                {
                    code: "2",
                    value: "2月份",
                },
                {
                    code: "3",
                    value: "3月份",
                },
                {
                    code: "4",
                    value: "4月份",
                },
                {
                    code: "5",
                    value: "5月份",
                },
                {
                    code: "6",
                    value: "6月份",
                },
                {
                    code: "7份",
                    value: "7月份",
                },
                {
                    code: "8",
                    value: "8月份",
                },
                {
                    code: "9",
                    value: "9月份",
                },
                {
                    code: "10",
                    value: "10月份",
                },
                {
                    code: "11",
                    value: "11月份",
                },
                {
                    code: "12",
                    value: "12月份",
                },
            ],
            isFlag: [
                {
                    code: "1",
                    value: "是",
                },
                {
                    code: "0",
                    value: "否",
                },
            ],
            securityList: [],
            key: 1, // table key
            tableList: [
                { label: "aqsjlx", value: "安全事件类型" },
                { label: "sjms", value: "事件描述" },
                { label: "zj", value: "组件" },
                { label: "fssj_year", value: "发生时间（年）" },
                { label: "fssj_month", value: "发生时间（月）" },
                { label: "fscs", value: "发生次数" },
                { label: "cxzsc", value: "持续总时长（分钟）" },
                { label: "pjcxsc", value: "平均持续时长(分钟)" },
                { label: "dqzt", value: "当前状态" },
                { label: "blxm", value: "暴露项目" },
                { label: "zyclfs", value: "主要处理方式" },
                { label: "sfjj", value: "是否解决" },
            ],
            checkList: [], //筛选列选中的数据列表
            columnShow: {}, //控制筛选列显示隐藏
            search:'',
            height: window.innerHeight - 159,
        };
    },
    methods: {
        
        //列筛选
        columnFilter(val) {
            debugger;
            if (val == undefined) {
                this.tableList.forEach((element) => {
                    // 默认选中全部列
                    this.checkList.push(element.label);
                    this.columnShow[element.label] = true;
                });
            } else {
                Object.keys(this.columnShow).forEach((element) => {
                    this.columnShow[element] = false;
                });
                this.checkList.forEach((element) => {
                    // 判断是否有某个字段
                    if (element in this.columnShow) {
                        this.columnShow[element] = true;
                    }
                });
            }
            this.key = this.key + 1;
        },
        queryList() {
            //查询
            debugger;
            let that = this;
            var year = this.formData.year.value;
            var month = this.formData.month.value;
            var isFlag = this.formData.isFlag.value;
            if (
                year !== undefined &&
                month != undefined &&
                isFlag != undefined
            ) {
                const loading = this.$loading({
                    lock: true,
                    text: "Loading",
                    spinner: "el-icon-loading",
                });
                that.$http({
                    method: "post",
                    url: querySecurityQuestionUrl,
                    baseURL: "/api",
                    data: {
                        year: year,
                        month: month,
                        isFlag: isFlag,
                    },
                })
                    .then(function (response) {
                        if (response.data.code == 1) {
                            that.securityList = response.data.data;
                            Message.success("查询成功");
                        } else {
                            Message.error("查询失败");
                        }
                        loading.close();
                    })
                    .catch(function (err) {
                        console.log(err);
                        Message.error("查询失败");
                        loading.close();
                    });
            } else {
                Message.warning("请选择查询条件");
            }
        },
       //导出Excel
        htmlToExcel(dom, title = "默认标题") {
            debugger;
            const wb = XLSX.utils.table_to_book(document.querySelector(dom));
            const wbout = XLSX.write(wb, {
                bookType: "xlsx",
                bookSST: true,
                type: "array",
            });
            const blob = new Blob([wbout], {
                type: "application/octet-stream",
            });
            FileSaver.saveAs(blob, `${title}.xlsx`);
        },
        //表头颜色
        getRowClass({ rowIndex }) {
            if (rowIndex == 0 || rowIndex == 1) {
                return "background:#2FB2B8;color: white";
            } else {
                return "";
            }
        },
        //Table高度
        getHeight() {
            let that = this;
            that.height = window.innerHeight - 159;
        },
    },
    mounted() {
        this.columnFilter();
    },
    //自适应高度
    created() {
        let that = this;
        window.addEventListener("resize", that.getHeight);
    },
    destroyed() {
        let that = this;
        window.removeEventListener("resize", that.getHeight);
    },
};
</script>

<style scoped>
.headerCoster {
    height: 59px;
    border-top: 1px solid #eee;
    border-bottom: 1px solid #eee;
}

.deleteButtonColor {
    color: red;
}

.submitButtonColor {
    color: green;
}

.letBorder {
    border-left: #6aabe6 solid 1px;
}

.def-el-input {
    -webkit-appearance: none;
    background-color: #fff;
    background-image: none;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
    box-sizing: border-box;
    color: #606266;
    display: inline-block;
    font-size: inherit;
    height: 40px;
    line-height: 40px;
    outline: none;
    padding: 0 15px;
    width: 35%;
}

.cardWidth {
    width: 100%;
}
.el-select {
    width: 100%;
}
.el-popover .el-checkbox {
  display: block;
  margin-bottom: 5px;
  margin-left: 15px;
}
</style>